﻿namespace ACCOUNTING.LVY.Business
{
    using ACCOUNTING;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Data;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    public class clsCapNhatThanhToanTheoMatHang
    {
        private double mDaThuTien = 0.0;
        private double mDaThuTienNT = 0.0;
        private string mKhoa = "";
        private string mKhoaNhanVienCapNhat = "";
        private string mKhoaPhieuXuat = "";
        private string mNgayCapNhat = "";
        private double mSoLuongThuTien = 0.0;

        public DataTable GetDetailsOfHoaDon(string strKhoa)
        {
            string commandText = " SELECT CT.Khoa, CT.KhoaHangHoa, HH.Ma As MaHang, HH.Ten" + modGeneral.H_LANGUAGE + " As TenHang,  CT.SoLoHang, dbo.Char2Date(CT.HanSuDung) As HanSuDung, DVT.Ten" + modGeneral.H_LANGUAGE + " As DonViTinh,  CT.SoLuong, CT.TienBan - CT.TienChietKhau + CT.TienThue As TriGia, CT.SoLuongThuTien As SoLuongThu, CT.DaThuTien As TriGiaThu FROM ST_XuatKhoChiTiet CT LEFT JOIN DM_HangHoa HH on CT.KhoaHangHoa = HH.Khoa LEFT JOIN DM_DonViTinh DVT ON CT.KhoaDonViTinh = DVT.Khoa  WHERE CT.KhoaPhieuXuat = '" + LVYModule.ReturnIfNull(strKhoa, "", modGeneral.TypeOfValue.Nothing).ToString().Trim() + "' ORDER BY CT.Khoa ";
            return DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
        }

        public DataTable GetListHoaDon(string strCondition = "")
        {
            DataTable table;
            string str2 = "";
            try
            {
                if (strCondition.Trim() != "")
                {
                    str2 = " WHERE " + strCondition;
                }
                string commandText = "SELECT XK.Khoa, XK.SoChungTu, dbo.Char2Date(XK.NgayChungTu) as Ngay, XK.SoHoaDon,  DT.Ten" + modGeneral.H_LANGUAGE + " as KhachHang,  XK.TienHangNT as TienHang , XK.TienChietKhauNT as TienChietKhau,  XK.TienThueVATNT as TienThue, XK.DaThanhToanNT -  XK.TienChietKhauNT + XK.TienThueVATNT As TongCong, XK.DaThanhToanNT As DaThanhToan,  XK.DaThanhToanNT -  XK.TienChietKhauNT + XK.TienThueVATNT - XK.DaThanhToanNT as ConLai  FROM ST_XuatKho XK LEFT  JOIN DM_DoiTuong DT ON XK.KhoaDoiTuong = DT.Khoa " + str2 + " ORDER BY XK.NgayChungTu, XK.SoChungTu";
                table = DBHelper.ExecuteDataset(modGeneral.ConnectionString, CommandType.Text, commandText, new IDataParameter[0]).Tables[0];
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message);
                table = null;
                ProjectData.ClearProjectError();
                return table;
                ProjectData.ClearProjectError();
            }
            return table;
        }

        public bool Save()
        {
            bool flag=false;
            IDataParameter[] commandParameters = new IDataParameter[8];
            try
            {
                commandParameters[0] = DBHelper.createParameter("@Khoa", DbType.Int64, ParameterDirection.Input, this.mKhoa);
                commandParameters[1] = DBHelper.createParameter("@KhoaPhieuXuat", DbType.Int64, ParameterDirection.Input, this.mKhoaPhieuXuat);
                commandParameters[2] = DBHelper.createParameter("@SoLuongThuTien", DbType.Date, ParameterDirection.Input, this.mSoLuongThuTien);
                commandParameters[3] = DBHelper.createParameter("@DaThuTienNT", DbType.Date, ParameterDirection.Input, this.mDaThuTienNT);
                commandParameters[4] = DBHelper.createParameter("@DaThuTien", DbType.Date, ParameterDirection.Input, this.mDaThuTien);
                commandParameters[5] = DBHelper.createParameter("@KhoaNhanVienCapNhat", DbType.Int64, ParameterDirection.Input, this.mKhoaNhanVienCapNhat);
                commandParameters[6] = DBHelper.createParameter("@NgayCapNhat", DbType.Int64, ParameterDirection.Input, this.mNgayCapNhat);
                commandParameters[7] = DBHelper.createParameter("@pError", DbType.Double, ParameterDirection.Output, 0);
                int num = DBHelper.ExecuteNonQuery(modGeneral.ConnectionString, CommandType.StoredProcedure, "BH_sp_XacNhanThanhToanTheoMatHang", commandParameters);
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectEqual(commandParameters[7].Value, 0, false))
                {
                    return true;
                }
                if (Microsoft.VisualBasic.CompilerServices.Operators.ConditionalCompareObjectNotEqual(commandParameters[7].Value, 0, false))
                {
                    LVYModule.ShowWarning(modGeneral.Tranlated("Nghiệp vụ kh\x00f4ng thể thực hiện được v\x00ec \x00e2m kho!", ""));
                }
                flag = false;
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exception = exception1;
                MessageBox.Show(exception.Message.ToString());
                flag = false;
                ProjectData.ClearProjectError();
                return flag;
                ProjectData.ClearProjectError();
            }
            return flag;
        }

        public double DaThuTien
        {
            get
            {
                return this.mDaThuTien;
            }
            set
            {
                this.mDaThuTien = value;
            }
        }

        public double DaThuTienNT
        {
            get
            {
                return this.mDaThuTienNT;
            }
            set
            {
                this.mDaThuTienNT = value;
            }
        }

        public string Khoa
        {
            get
            {
                return this.mKhoa;
            }
            set
            {
                this.mKhoa = value;
            }
        }

        public string KhoaNhanVienCapNhat
        {
            get
            {
                return this.mKhoaNhanVienCapNhat;
            }
            set
            {
                this.mKhoaNhanVienCapNhat = value;
            }
        }

        public string KhoaPhieuXuat
        {
            get
            {
                return this.mKhoaPhieuXuat;
            }
            set
            {
                this.mKhoaPhieuXuat = value;
            }
        }

        public string NgayCapNhat
        {
            get
            {
                return this.mNgayCapNhat;
            }
            set
            {
                this.mNgayCapNhat = value;
            }
        }

        public double SoLuongThuTien
        {
            get
            {
                return this.mSoLuongThuTien;
            }
            set
            {
                this.mSoLuongThuTien = value;
            }
        }
    }
}

